package club.hicode.daydayup.oa;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import okhttp3.*;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * OA自动处理器
 *
 * @author Liu Chunfu
 * @date 2018-08-01 下午7:29
 **/
public class OAAutoComputer {

	@Test
	public void solveOA() throws Exception {
		String jsessionId = "9DEC86685AE9BF6B3089ECFC15D80466";
		OkHttpClient mOkHttpClient = initClient(jsessionId);
		Request request = getRequest("http://oa.centriole.cn/hrms/rest/workflowManage/getTodoTasks");
		//		新的请求获取所有的待审核列表
		Call call = mOkHttpClient.newCall(request);
		Response response = call.execute();
		ResponseBody body = response.body();
		String jsonStr = new String(body.bytes(), CharsetUtil.CHARSET_UTF_8);
		JSONArray objects = JSONUtil.parseArray(jsonStr);
		for (Object object : objects) {
			JSONObject obj = (JSONObject) object;
			String taskId = Convert.toStr(obj.get("taskId"));
			String applyUserName = Convert.toStr(obj.get("applyUserName"));
			String type = Convert.toStr(obj.get("proccessKey"));
			String template = "====={}======\n{} \n{} \n";
			JSONObject innerObj = (JSONObject) obj.get("variables");
			Object businessDesc = innerObj.get("businessDesc");
			if (type.equals("overtime")) {
				Call overtimeCall = mOkHttpClient
						.newCall(postRequest("http://oa.centriole.cn/hrms/rest/overtime/handlingOverTimeTask", taskId));
				Response postResponse = overtimeCall.execute();
				String postStr = postResponse.body().string();
				System.out.println(StrUtil.format(template, applyUserName, businessDesc, postStr));

			} else if (type.equals("repairpunch")) {
				Call reqaireCall = mOkHttpClient
						.newCall(postRequest("http://oa.centriole.cn/hrms/rest/punch/repair/appr", taskId));
				Response postResponse = reqaireCall.execute();
				String postStr = postResponse.body().string();
				System.out.println(StrUtil.format(template, applyUserName, businessDesc, postStr));
			} else if (type.equals("Leave")) {
				Call reqaireCall = mOkHttpClient
						.newCall(postRequest("http://oa.centriole.cn/hrms/rest/leaverequest/appr", taskId));
				Response postResponse = reqaireCall.execute();
				String postStr = postResponse.body().string();
				System.out.println(StrUtil.format(template, applyUserName, businessDesc, postStr));
				//
			}
			ThreadUtil.sleep(3, TimeUnit.SECONDS);
		}

	}

	/**
	 * 创建客户端
	 *
	 * @param jsessionId
	 * @return
	 */
	private OkHttpClient initClient(String jsessionId) {
		OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS)
				.cookieJar(new CookieJar() {//这里可以做cookie传递，保存等操作
					@Override
					public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {//可以做保存cookies操作
					}

					@Override
					public List<Cookie> loadForRequest(HttpUrl url) {
						ArrayList<Cookie> cookies = new ArrayList<>();
						Cookie cookie = new Cookie.Builder().hostOnlyDomain(url.host()).name("JSESSIONID")
								.value(jsessionId).build();
						cookies.add(cookie);
						return cookies;
					}
				}).build();
		return httpClient;
	}

	/**
	 * get请求
	 *
	 * @param url
	 * @return
	 */
	private Request getRequest(String url) {
		Request request = new Request.Builder().url(url).addHeader("ContentType", "application/json; charset=UTF-8")
				.header("Accept", "application/json").build();
		return request;
	}

	/**
	 * post请求
	 *
	 * @param url
	 * @param taskId
	 * @return
	 */
	private Request postRequest(String url, String taskId) {
		RequestBody formBody = new FormBody.Builder().add("taskId", taskId).add("pass", "1").build();
		Request request = new Request.Builder().url(url).addHeader("ContentType", "application/json; charset=UTF-8")
				.header("Accept", "application/json").post(formBody).build();
		return request;
	}
}
